package com.zhuannimei.parser;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zhuannimei.domain.Product;

/**
 * 商品列表解析
 * 
 * @author Jack 2013-12-8
 *  "code": "0",
    "desc": "操作成功",
    "total":2,
    "result": [{
        "productId":"商品ID",
        "title": "商品名称",
        "price": "价格",
        "picture": "图片地址",
        "describe":"描述",
        "total":"总数量", new
        "last":"剩余数量", new
        "buyed":"兑换积分",
        "ctime":"上架时间"
    },
 * 
 */
public class ProductParser extends BaseJSONParser<List<Product>> {

	@Override
	public List<Product> parseJSON(String jsonstr) throws JSONException {
		List<Product> list = new ArrayList<Product>();
		JSONObject jo = new JSONObject(jsonstr);
	//获取总页数
		JSONArray jsonArray = jo.getJSONArray("result");
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject obj = jsonArray.getJSONObject(i);
			Product pro = new Product();
			pro.setId(obj.optString("productId"));
			pro.setName(obj.optString("title"));
			pro.setPhoto(obj.optString("picture"));
			pro.setPrice(obj.optString("price"));
			pro.setDescribe(obj.optString("describe"));
			pro.setTotal(obj.optString("total"));
			pro.setLast(obj.optString("last"));
			pro.setBuyed(obj.optString("buyed"));
			pro.setCtime(obj.optString("ctime"));
			list.add(pro);
		}
		return list;
	}

}
